package com.egao.common.module.system.service.impl;

import cn.hutool.core.io.FileUtil;
import com.egao.common.module.system.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * 发送邮件
 *
 * @author system
 */
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    /**
     * 发件人
     */
    @Value("${spring.mail.title}")
    private String sendTitle;
    @Value("${spring.mail.username}")
    private String formEmail;

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendTextEmail(String title, String content, String[] toEmails) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(formEmail);
        message.setTo(toEmails);
        message.setSubject(sendTitle + " - " + title);
        message.setText(content);
        try {
            mailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败 sendTextEmail", e);
        }
    }

    @Override
    public void sendFullTextEmail(String title, String html, String[] toEmails) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(formEmail);
        helper.setTo(toEmails);
        helper.setSubject(sendTitle + " - " + title);
        // 发送邮件
        helper.setText(html, true);
        try {
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error("邮件发送失败 sendFullTextEmail", e);
        }
    }

    @Override
    public void sendHtmlEmail(String title, String htmlPath, Map<String, Object> map, String[] toEmails) throws MessagingException, IOException {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("templates/");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        // 加载html模板
        Template t = gt.getTemplate(htmlPath);
        // 填充数据
        t.binding(map);
        // 获得渲染后的html
        String html = t.render();
        // 发送邮件
        sendFullTextEmail(title, html, toEmails);
    }

    @Override
    public void sendFileEmail(String title, String companyName, String studentName, String pushTitle, File file, String toEmails) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(formEmail);
        helper.setTo(toEmails);
        helper.setSubject(sendTitle + " - " + title);

        String text = "“" + companyName + "”您好，我校学生“" + studentName + "”对您在本校发布的“" + pushTitle + "”中的岗位很有意愿且匹配度较高，特向您发送该学生的简历供您筛选，简历在邮件附件中下载。注：此邮件由系统帮助发送，不接受回复，如有意愿可在简历中找到学生联系方式进行联系。谢谢！";
        helper.setText(text);

        String suffix = "." + FileUtil.getSuffix(file);
        String fileName = studentName + "的简历" + suffix;
        helper.addAttachment(fileName, file);

        try {
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            log.error("邮件发送失败 sendFileEmail", e);
        }
    }

}
